package org.smart4j.system.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.framework.annotation.Action;
import org.smart4j.framework.annotation.Controller;
import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.bean.Param;
import org.smart4j.framework.bean.View;
import org.smart4j.framework.helper.BeanHelper;
import org.smart4j.plugin.security.SecurityHelper;
import org.smart4j.plugin.security.annotation.User;
import org.smart4j.plugin.security.exception.AuthcException;
import org.smart4j.system.entity.UserBean;
import org.smart4j.system.service.UserService;

import java.util.Map;

/**
 * Created by Wangminze on 2016/12/31.
 * Description:处理系统请求
 */
@Controller
public class SystemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemController.class);

    @Inject
    private UserService userService;

    /**
     * author: Wangminze
     * date: 2016/12/31
     * description: 进入首页界面
     * @return
     */
    @Action("get:/")
    public View index(){
        return new View("/index");
    }

    /**
     * author: Wangminze
     * date: 2016/12/31
     * description:进入登录页面
     * @return
     */
    @Action("get:/login")
    public View login(){
        LOGGER.debug("用户登录");
        return new View("login");
    }

    /**
     * author: Wangminze
     * date: 2017/3/25
     * description: 用户注册跳转
      * @return
     */
     @Action("get:/register")
    public View register(){
        LOGGER.info("用户注册");
        return new View("register");
    }

    /**
     * author: Wangminze
     * date: 2016/12/31
     * description:提交登录表单
     * @return
     */
    @Action("post:/register")
    public View registerSubmit(Param param){
        LOGGER.info("用户注册验证");
        String username = param.getString("username");
        String password = param.getString("password");
        String passwordSure = param.getString("password_sure");

        if ( "".equals(password) || "".equals(passwordSure)
                || !password.equals(passwordSure)){
            View v = new View("register");
            v.addModel("errorMsg","两次密码不一致");
            return v;
        }

        try {

            UserBean user = new UserBean();
            user.setUsername(username);
            user.setPassword(password);
            Map<Class<?>,Object> beans = BeanHelper.getBeanMap();
            System.out.println(beans.toString());
            userService.addUser(user);

        }catch (AuthcException e){
            LOGGER.error("Register failure", e);
            View v = new View("register");
            v.addModel("errorMsg","系统发生异常");
            return v;
        }
        return new View("login");
    }

    /**
     * author: Wangminze
     * date: 2016/12/31
     * description:提交登录表单
     * @return
     */
    @Action("post:/login")
    public View loginSubmit(Param param){
        LOGGER.info("用户登录验证");
        String username = param.getString("username");
        String password = param.getString("password");

        try {
            SecurityHelper.login(username, password);
        }catch (AuthcException e){
            LOGGER.error("登录失败", e);
            View v = new View("login");
            v.addModel("errorMsg","用户名或密码错误");
            return v;
        }
        return new View("customer");
    }

    /**
     * author: Wangminze
     * date: 2016/12/31
     * description:提交注销请求
     * @return
     */
    @User
    @Action("get:/logout")
    public View logout(){
        LOGGER.info("用户退出登录");
        SecurityHelper.logout();
        return new View("/index");
    }
}
